package ch05;

import com.sun.org.apache.xpath.internal.operations.Plus;
import lombok.SneakyThrows;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Msg {
    public double i;
    public double j;
    public String orgStr = null;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 1; i < 1000; i++) {
            for (int i1 = 1; i1 < 1000; i1++) {
                Msg msg = new Msg();
                msg.i = i;
                msg.j = i1;
                msg.orgStr = "((" + i + "+" + i1 + ")*" + i + ")/2";
                Add.queue.add(msg);
            }
        }
        Thread thread1 = new Thread(new Add());
        Thread thread = new Thread(new Mutiply());
        Thread thread2 = new Thread(new Div());
        thread1.start();
        thread.start();
        thread2.start();




    }
}

class Add implements Runnable {
    public static BlockingQueue<Msg> queue = new LinkedBlockingQueue<>();

    @Override
    public void run() {
        while (true) {
            Msg take = null;
            try {
                take = queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            take.j = take.i + take.j;
            Mutiply.queue.add(take);
        }
    }
}
class Mutiply implements Runnable{
    public static BlockingQueue<Msg> queue = new LinkedBlockingQueue<>();

    @Override
    public void run() {
        while (true) {
            Msg mul = null;
            try {
                mul = queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            mul.i = mul.i * mul.j;
            queue.add(mul);
        }
    }
}

class Div implements Runnable {
    public static BlockingQueue<Msg> queue = new LinkedBlockingQueue<>();


    @Override
    public void run() {
        while (true) {
            Msg div = null;
            try {
                div = queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            div.i = div.i / 2;
            System.out.println(div.orgStr + "=" + div.i);
        }
    }
}
